package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.bsh.mybatis.IRepository.AccountChangeAuditMapper;
import com.bsh.mybatis.model.AccountChangeAudit;
import com.bsh.utils.Pager;

/**
 * 后台变动账户审核
 * @author gsh
 *
 */
@Repository
public class DaoAccountChangeAudit {
	private static Logger log = LoggerFactory.getLogger(DaoAccountChangeAudit.class);
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;


	AccountChangeAuditMapper getMapper() {
		return session.getMapper(AccountChangeAuditMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(AccountChangeAudit record) {
		getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(AccountChangeAudit record) {
		getMapper().insertSelective(record);
		return record.getId();
	}

	public AccountChangeAudit selectByPrimaryKey(Integer id) {
		AccountChangeAudit row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(AccountChangeAudit record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(AccountChangeAudit record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}
	
	
	/**
	 * 变动申请列表
	 */
	public List<Map<String,Object>> getPage(Pager pager,String where) {				
		StringBuilder sb=new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();	
		sb.append("select * from (");
		sb.append("select c.*,a.login_name auditor_name,u.name user_name,u.mobile ");
		sb.append(" from account_change_audit c");
		sb.append(" left join admin a on a.id=c.auditor ");
		sb.append(" left join user u on u.id=c.userid ");
		sb.append(" )v ");	
		sb.append(" where 1=1 ");	
		if(!StringUtils.isEmpty(where)){
			sb.append(where);
		}
		Integer count= namedJdbcTemplate.queryForInt("select count(0) from ("+sb.toString()+")v",ps);

		sb.append(" order by id desc limit :start,:size");	
		
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String,Object>> list=namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	/**
	 * 审核通过 
	 * @return
	 */
	public Integer auditPass(Integer id,Integer auditorId){
		int res = getMapper().auditPass(id,auditorId);
		return res;
	}
	
	/**
	 * 审核不通过 
	 * @return
	 */
	public Integer auditRefuse(Integer id,Integer auditorId){
		int res = getMapper().auditRefuse(id,auditorId);
		return res;
	}
	/**
	 * 经理审核_通过 
	 * @return
	 */
	public Integer managerAuditPass(Integer id,Integer auditorId){
		int res = getMapper().managerAuditPass(id,auditorId);
		return res;
	}
	
	/**
	 * 经理审核_拒绝
	 * @return
	 */
	public Integer managerAuditRefuse(Integer id,Integer auditorId){
		int res = getMapper().managerAuditRefuse(id,auditorId);
		return res;
	}
}
